var app = getApp(),
  common = require("../../common/common.js"),
  WxParse = require('../../../../wxParse/wxParse.js')

// 答题结果
var selected_all = [] //全部答案
var selected_true = [] //统计正确答案个数
var selected_false = [] //统计错误答案个数

Page({
  data: {
    listData: [],
    options: 0,
  },
  // 题
  current(e) {
    console.log(e.detail.current)
    let _t = this
    _t.setData({
      options: e.detail.current
    })
  },

  // // 禁止 sw-item 滑动
  // stopTouchMove() {
  //   return false;
  // },

  // 下一题
  nextOptions(e) {
    let _t = this
    console.log(e)
    let index = e.currentTarget.dataset.index
    let correct = e.currentTarget.dataset.correct
    let len = e.currentTarget.dataset.len

    _t.data.options++

    _t.setData({
      options: _t.data.options
    })
  },
  
  // 题目
  startTest(f_ques, _t) {
    
    var startTest = []
    var analysis = []

    app.util.request({
      url: 'entry/wxapp/wrongTopicParsing',
      data: {
        ErrID: f_ques
      },
      success: function (a) {
        console.log(a)
        let data = a.data.data
        console.log(data)
        for (var i = 0; i < data.length; i++) {
          data[i].answer_option = JSON.parse(data[i].answer_option);
          data[i].correct = data[i].correct.split(',')

          // 选项数据
          let answer_option = []
          for (var v = 0; v < data[i].answer_option.length; v++) {
            answer_option.push({
              name: data[i].answer_option[v],
              active: false
            })
          }
          data[i].answer_option = answer_option;

          // 答案数据
          for (var k = 0; k < data[i].correct.length; k++) {
            data[i].correct[k] = Number(data[i].correct[k]) + 1
          }
          // 数据 定义
          startTest.push(data[i].subject)
          // 讲解 定义
          analysis.push(data[i].analysis)

          WxParse.wxParse('explain' + i, 'html', data[i].analysis, _t); //讲解
          WxParse.wxParse('subject' + i, 'html', data[i].subject, _t); //题目
          if (i === data.length - 1) {
            WxParse.wxParseTemArray("analysis", 'explain', data.length, _t) //讲解
            WxParse.wxParseTemArray("startTest", 'subject', data.length, _t) //题目
          }

        }
        data = data
        _t.setData({
          test: data
        })
      }
    })
  },
  // 收藏 || 取消收藏 题目
  collection(e) {
    console.log(e.currentTarget.dataset.sid)
    let _t = this
    let sid_index = e.currentTarget.dataset.sid
    let sid = _t.data.test[sid_index].id
    let openid = _t.data.users.openid

    app.util.request({
      url: 'entry/wxapp/questionCollect',
      data: {
        sid: sid,
        openid: openid
      },
      success: function (a) {
        console.log(a)
        console.log("=///////////////////////////////////////////////////////")
        wx.showToast({
          title: a.data.message,
          icon: 'none',
          duration: 1500,
        })
      }
    })

  },
  // 获取用户信息
  haveGetUser(_t) {

    console.log(wx.getStorageSync("users"))
    _t.setData({
      users: wx.getStorageSync("users")
    })
  },
  onLoad: function (w) {
    let _t = this
    // 获取手机屏幕高度
    let winh = wx.getSystemInfoSync().windowHeight
   
    _t.setData({    
      winh: winh,
      id: w.id,
    });

    _t.startTest(w.id,_t)
    _t.haveGetUser(_t)

  },

  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})